module AuthenticatorPcr
	module Routing
		# Loads the set of routes from within a plugin and
		# evaluates them at this point within an application's
		# main routes.rb file.
		def from_plugin(name, controller_name, session_name, options={})
			plugin_root = File.join(RAILS_ROOT,
															'vendor',
															'plugins')
			routes_path = File.join(plugin_root,
															name.to_s,
															'routes.rb')
			if File.file?(routes_path)
				controller_file_path, controller_file_name = extract_modules controller_name
				session_file_path, session_file_name = extract_modules session_name
				https = options[:user_https] ? 'https' : 'http'
				eval(IO.read(routes_path), binding, routes_path)
			end
		end

		def extract_modules(name)
			modules = name.include?('/') ? name.split('/') : name.split('::')
			path = modules.map{|m| m.underscore }
			[File.join(path), path.pop]
		end
	end
end